"""
Case Type   : spq多机并行
Case Name   : gs_guc reload 设置comm_control_port参数,重启集群生效
Create At   : 2023/12/15
Owner       : songjing20
Description :
    1.查看comm_control_port的值
    2.设置为7001
    3.设置为初始值
    4.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
History     :
"""

import os
import unittest

from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SpqPlugin(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common('PrimaryDbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.pri_node = Node('PrimaryDbUser')
        self.constant = Constant()
        self.param = 'comm_control_port'

    def test_spq(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        text = '-----step1:查看comm_control_port的值;expect:成功-----'
        self.log.info(text)
        self.para_value = self.common.show_param(self.param)
        self.log.info(self.para_value)
        self.assertEqual(str(int(self.pri_node.db_port) + 40),
                         self.para_value, f'执行失败:{text}')

        text = '---step2:设置7001，重启集群;expect:成功---'
        self.log.info(text)
        res = self.pri_sh.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, f"{self.param}=7001")
        self.log.info(res)
        self.assertTrue(res, f'执行失败:{text}')
        res = self.common.show_param(self.param)
        self.log.info(res)
        self.assertIn('38740', res, f'执行失败:{text}')
        self.pri_sh.restart_db_cluster()
        res = self.common.show_param(self.param)
        self.assertEqual(res, '7001', f'执行失败:{text}')

        text = '-----step3:设置为初始值;expect:成功-----'
        self.log.info(text)
        res = self.pri_sh.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            f"{self.param}={self.para_value}")
        self.log.info(res)
        self.assertTrue(res, f'执行失败:{text}')
        self.pri_sh.restart_db_cluster()
        res = self.common.show_param(self.param)
        self.assertEqual(res, self.para_value, f'执行失败:{text}')

    def tearDown(self):
        text = '-----step4:清理环境;expect:成功-----'
        self.log.info(text)
        res = self.pri_sh.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f"{self.param}={int(self.pri_node.db_port) + 40}")
        self.log.info(res)
        restart_res = self.pri_sh.restart_db_cluster()
        self.log.info(restart_res)
        self.assertTrue(res, f'执行失败:{text}')
        self.assertTrue(restart_res, f'执行失败:{text}')
        self.log.info(f'----{os.path.basename(__file__)} end-----')
